* chetverikov.do	JEP	03/11/15
* Implements group level regressor quantile estimator - Chetverikov et. al.

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")	
}

cap log close
log using "`master'/Code/Est/chetverikov.log" , text replace


*/


* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"


use "`master'/Data/Source/ocupados_mw.dta", clear

* Choose quantiles
glo qtiles "5 10 15 20 25 30 50 75 90"
* glo qtiles "5 10"
* glo qtiles "90"
* Number of bootstraps
glo boot=10

* RIF 4 - Tables numbered 4 in term paper

* Choose sample
mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
tab rif4sample
tab tipo_trabajador if rif4sample
tab year if rif4sample
tab horas_semana if rif4sample 
tab sex if rif4sample
tab afiliado_salud if rif4sample
* Build mw measures
mw_measures rif4sample , stub(rif4)
* Build treatment
mytreatment, posts("tq(1998q3)") mw(bite_rif4_ind) wage(salario_mensual_real)

* Regressions

* Collapse at the state - cell level

glo plist ""
foreach q in $qtiles {
	glo plist "$plist (p`q') q`q'_lw=lw"
}	

collapse (firstnm) emp bartikp1_ex bartikq_ex trend treat $plist, by(ccode time cactividad_empresa)

est clear
foreach q in $qtiles {
	qui reg q`q'_lw i.time emp treat i.ccode##i.cactividad_empresa bartikp1_ex , r
	est store q`q'
}
est tab *, keep(treat) b se
est tab *, keep(treat) b star(0.1 0.05 0.01)

log close